package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Module;
import cn.itcast.service.system.ModuleService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.Md5Utils;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {
    @Autowired
    private ModuleService moduleService;

    /**
     * 分页查询所有模块信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list")
    public String list(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "5") int pageSize){
        PageInfo<Module> pageInfo = moduleService.findByPage(pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "/system/module/module-list";
    }

    /**
     * 查询可选父模块，跳转到添加模块页面
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(){
        List<Module> menus = moduleService.findAll();
        request.setAttribute("menus",menus);
        return "system/module/module-add";
    }

    /**
     * 判断id是否为空值，如果为空值则进行保存操作，反之进行更新操作
     * @param module
     * @return
     */
    @RequestMapping("edit")
    public String edit(Module module){

        if (StringUtils.isEmpty(module.getId())){
            moduleService.save(module);
        }else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }

    /**
     * 查询指定id模块的信息在更新页回显
     * @param id
     * @return
     */
    @RequestMapping("toUpdate")
    public String update(String id){
        Module module = moduleService.findById(id);
        request.setAttribute("module",module);
        List<Module> menus = moduleService.findAll();
        request.setAttribute("menus",menus);
        return "/system/module/module-update";
    }

    /**
     * 根据删除模块
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public String delete(String id){
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }
}
